import axios from "axios"

const service = axios.create({
  baseURL: 'https://test-ijournal-service.topeditsci.com',
  timeout: 5000
})
service.interceptors.request.use(
  config => {
    
    if(config.method === 'get' && config.data ){
      const queryString = new URLSearchParams(config.data).toString()
      config.url = config.url.includes('?') ? `&${queryString}` : `${config.url}?${queryString}`
    }
    
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
service.interceptors.response.use(function (response) {
  if (response.data.state === false) {
    localStorage.clear()
    location.replace('/')
  } else {
    return response
  }
})
export const Method = {
  GET: 'get',
  POST: 'post',
  PUT: 'put',
  DELETE: 'delete'
}
export default service